package com.whfc.fmam.redis.impl;

import com.whfc.fmam.entity.bo.FmamMatType;
import com.whfc.fmam.entity.dto.FmamMatTypeDTO;
import com.whfc.fmam.redis.FmamMatTypeRedisDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

/**
 * 材料类型Redis存储
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020/6/3 18:08
 */
@Component
public class FmamMatTypeRedisDaoImpl implements FmamMatTypeRedisDao {

    private final String KEY = "fmam::mat_type::{0}";

    @Autowired
    private RedisTemplate<String, FmamMatTypeDTO> redisTemplate;

    @Override
    public void put(Integer deptId, Long matTypeId, FmamMatType matType) {
        String key = MessageFormat.format(KEY, String.valueOf(deptId));
        String hk = String.valueOf(matTypeId);
        FmamMatTypeDTO matTypeDTO = to(matType);
        redisTemplate.opsForHash().put(key, hk, matTypeDTO);
    }

    @Override
    public FmamMatTypeDTO get(Integer deptId, Long matTypeId) {
        String key = MessageFormat.format(KEY, String.valueOf(deptId));
        String hk = String.valueOf(matTypeId);
        return (FmamMatTypeDTO) redisTemplate.opsForHash().get(key, hk);
    }

    @Override
    public void del(Integer deptId, Long matTypeId) {
        String key = MessageFormat.format(KEY, String.valueOf(deptId));
        String hk = String.valueOf(matTypeId);
        redisTemplate.opsForHash().delete(key, hk);
    }


    private FmamMatTypeDTO to(FmamMatType fmamMatType) {
        FmamMatTypeDTO matTypeDTO = new FmamMatTypeDTO();
        BeanUtils.copyProperties(fmamMatType, matTypeDTO);
        return matTypeDTO;
    }
}
